using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace MsdnVideo
{
    public partial class NuevoSocioForm : PlantillaForm
    {
        public NuevoSocioForm()
        {
            InitializeComponent();
        }
        private void NuevoSocioForm_Load(object sender, EventArgs e)
        {
            this.sociosBindingSource.AddNew();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (this.Validate())
            {
                this.sociosBindingSource.EndEdit();
                this.sociosTableAdapter.Update(this.sociosDS.Socios);
                MessageBox.Show("Socio insertado correctamente");
                this.sociosBindingSource.AddNew();
            }
            else
            {
                System.Windows.Forms.MessageBox.Show(this, "Validation errors occurred.", "Save", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning);
            }
        }

        private void dniMaskedTextBox_Validating(object sender, CancelEventArgs e)
        {
            string Letras = "TRWAGMYFPDXBNJZSQVHLCKE";
            int numNif;
            string nif = dniMaskedTextBox.Text;
            bool Valido = true;
            if (!System.Text.RegularExpressions.Regex.IsMatch(nif, @"^\d{8}[" + Letras + "]$"))
                Valido = false;
            else
            {
                numNif = Int32.Parse(nif.Substring(0, nif.Length - 1));
                if (!(nif[8] == Letras[numNif % 23] ))
                    Valido = false;
            }

            if (!Valido)
            {
                e.Cancel = true;
                errorProvider1.SetError(dniMaskedTextBox, "La letra del DNI no es correcta");
            }
            else
            {
                e.Cancel = false;
                errorProvider1.SetError(dniMaskedTextBox,string.Empty);
            }
        }

        private void nombreTextBox_Validating(object sender, CancelEventArgs e)
        {
            if (nombreTextBox.Text.Length == 0)
            {
                e.Cancel = true;
                errorProvider1.SetError(nombreTextBox, "Introduzca un nombre");
            }
            else
            {
                e.Cancel = false;
                errorProvider1.SetError(nombreTextBox, string.Empty);
            }
        }

        private void apellidosTextBox_Validating(object sender, CancelEventArgs e)
        {
            if (apellidosTextBox.Text.Length == 0)
            {
                e.Cancel = true;
                errorProvider1.SetError(apellidosTextBox, "Introduzca los apellidos");
            }
            else
            {
                e.Cancel = false;
                errorProvider1.SetError(apellidosTextBox, string.Empty);
            }
        }

        private void emailTextBox_Validating(object sender, CancelEventArgs e)
        {
            if (emailTextBox.Text.Length == 0)
            {
                e.Cancel = true;
                errorProvider1.SetError(emailTextBox, "Introduzca un email");
            }
            else
            {
                e.Cancel = false;
                errorProvider1.SetError(emailTextBox, string.Empty);
            }
        }



    }
}